﻿using Gadgeteer.Interfaces.Fakes;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Gadgeteer.Interfaces.Sample.Safe.SafeApp.Tests
{
    [TestFixture]
    public class CombinationEnteringTest
    {
        [TestFixtureSetUp]
        public void TestFixtureSetUp()
        {
            ServiceLocator.Locator.Setup();
        }

        [Test]
        public void OnCorrectCodeUnlockDoor()
        {
            var code = new [] { 0, 1, 0 };
            var padLock = new Lock(code);
            var led = new FakeMultiColorLed();
            var door = new MultiColorLedAsDoor(padLock, led);

            using (var nob1 = new FakeButton())
            using (var nob2 = new FakeButton())
            using (var nob3 = new FakeButton())
            {
                var lockKeeper = new CombinationEntering(padLock, nob1, nob2, nob3);
                Assert.That(lockKeeper.IsOpen, Is.False);

                nob1.Press();
                nob2.Press();
                nob1.Press();

                Thread.Sleep(200);
                Assert.That(lockKeeper.GetCurrentCode(), Is.EqualTo(new[] { 0, 1, 0 }));
                Assert.That(lockKeeper.IsOpen, Is.True);
            }
        }

        [Test]
        public void ResetNobs()
        {
            var code = new[] { 0, 1, 0 };
            var padLock = new Lock(code);
            var led = new FakeMultiColorLed();
            var door = new MultiColorLedAsDoor(padLock, led);

            using (var nob1 = new FakeButton())
            using (var nob2 = new FakeButton())
            using (var nob3 = new FakeButton())
            {
                var lockKeeper = new CombinationEntering(padLock, nob1, nob2, nob3);
                Assert.That(lockKeeper.IsOpen, Is.False);

                nob1.Press();
                nob2.Press();
                nob1.Press();

                Thread.Sleep(100);
                Assert.That(lockKeeper.GetCurrentCode(), Is.EqualTo(new[] { 0, 1, 0 }));


                Thread.Sleep(100);
                Assert.That(lockKeeper.GetCurrentCode(), Is.EqualTo(new[] { 0, 1, 0 }));
            }
        }
    }
}
